package com.克隆02.深克隆04;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

/**
 * 克隆实现方式一：实现cloneable接口，并重写Object的clone方法:
 * 浅克隆
 */
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Student implements Cloneable{
    private int age;
    private String name;

    private Teacher teacher;
    @Override
    protected Object clone() throws CloneNotSupportedException {
        Student s1 = (Student) super.clone();
        s1.setTeacher((Teacher) s1.getTeacher().clone());
        return s1;
    }

    public static void main(String[] args) throws Exception{
        Teacher t1 = new Teacher("张老师");
        Student s1 = new Student(1001, "李四", t1);

        Student s2 = (Student) s1.clone();

        //修改s2中的老师的姓名,然后打印比较
        s2.setName("李四同学");
        s2.getTeacher().setTeaName("1111老师");

        System.out.println("学生姓名:"+s1.getName()+",老师姓名:"+s1.getTeacher().getTeaName());//李四    张老师
        System.out.println("学生姓名:"+s2.getName()+",老师姓名:"+s2.getTeacher().getTeaName());//李四同学   1111老师

    }




}
